package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //1：套餐-分页查询
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //小插件自动写limit --- 分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageTao(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //2:套餐-新增套餐
    @Transactional
    @Override
    public void addTao(SetmealDTO setmealDTO) {
        //把套餐数据上传
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addTao(setmeal);
        //把套餐包含的菜品上传
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setleamId = setmeal.getId();
        for (SetmealDish setmealDish : setmealDishes) {
            //要把套餐id传入进去
            setmealDish.setSetmealId(setleamId);
            setmealDishMapper.addTaoDish(setmealDish);
        }

    }

    //3:套餐-根据id查询套餐
    @Override
    public SetmealVO getById(Long id) {
        //根据id返回套餐
        Setmeal setmeal = setmealMapper.getById(id);
        //根据id返回关联的套餐
        List<SetmealDish> setmealDish = setmealDishMapper.getTao(id);
        //把两个sql数据传输到setmealVO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    //4:套餐-批量删除套餐
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //起售中的套餐不能删除
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.getById(id);
            if (StatusConstant.ENABLE.equals(setmeal.getStatus())){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //若可以删除则也需要删除关联的
        ids.forEach(id->{
            setmealMapper.deleteBatch(id);
            setmealDishMapper.deleteBatchDish(id);
        });

    }

    //5:套餐-修改菜品
    @Transactional
    @Override
    public void putGai(SetmealDTO setmealDTO) {
        //把数据拷贝到setmeal里面
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //获取套餐id
        Long setmealId = setmeal.getId();
        //删除联系的套餐数据
        setmealDishMapper.deleteBatchDish(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDishe->{
            setmealDishe.setSetmealId(setmealId);
            //添加信息
            setmealDishMapper.addTaoDish(setmealDishe);
        });
    }

    //6:套餐-起售停售
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售之前要判断关联的菜品中有没有停售的菜品
        if (status.equals(StatusConstant.ENABLE)){
            //获取sql表里面->setmeal_dish的dish_id     因为我要的dish里面status看他的是不是起售状态，所以返回值设为dish
            List<Dish> list = setmealMapper.getByIdSetmealAndDish(id);
            if (list != null && list.size()>0){
                list.forEach(dish -> {
                    Integer status1 = dish.getStatus();
                    if (status1.equals(StatusConstant.DISABLE)){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }


        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.startOrStop(setmeal);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
